#pragma once
#include <iostream>
#include <string>
#include <functional>
#include "Inetaddr.hpp"
#include "Reactor.hpp"
#include "Socket.hpp"
class Connection;
class Reactor;
using func_t = std::function<void(std::shared_ptr<Connection>)>;

//上层只有Connection，没有fd的概念
class Connection
{
    public:
        Connection(socket_ns::socket_ptr sock) : _sock(sock)
        {}
        ~Connection(){}
        socket_ns::socket_ptr Sock()
        {
            return _sock;
        }
        uint32_t Events()
        {
            return _events;
        }
        void SetEvents(int events)
        {
             _events = events;
        }
        void Register(func_t recver , func_t sender , func_t excepter)
        {
            _recver = recver;
            _sender = sender;
            _excepter = excepter;
        }
        void SetSelf(Reactor* R)
        {
            _R = R;
        }
        void AppendInBuffer(const std::string& str)
        {
            _inbuffer += str;
        }
        std::string InBuffer()
        {
            return _inbuffer;
        }
    private:
        socket_ns::socket_ptr _sock;
        std::string _inbuffer; //输入缓冲区
        std::string _outbuffer; //输出缓冲区
        InetAddr _addr; //sock对应的端口号和ip地址
        uint32_t _events; // Connection对象中，_sock关心的事件集合

    public:
        func_t _recver; //读取处理
        func_t _sender; //发送处理
        func_t _excepter; //异常处理
        Reactor* _R; //用于链接新增时的AddConnection操作
};